package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.MD5;
import com.itheima.entity.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
/**
 *用户模块业务层实现类
 * @author msi
 * @date 2022/5/17
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper mapper;

    @Override
    public User login(String phone) {

        User userOld = mapper.selectByPhone(phone);
        if (userOld != null) {
            return userOld;
        }

        User user = new User();
        user.setPhone(phone);
        Snowflake snowflake = IdUtil.getSnowflake();
        String id = snowflake.nextIdStr();
        String username = RandomUtil.randomString(10);
        user.setUsername(username);
        MD5 md5 = MD5.create();
        String password = md5.digestHex("123456");
        user.setPassword(password);
        user.setId(id);
        user.setStatus(1);

        mapper.addUser(user);
        return user;
    }
}
